package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public List<Cart> listCart(String userId, Integer businessId) throws Exception {
        String sql = "select c.*,b.*,f.* from cart c,business b,food f where c.foodId = f.foodId and " +
                " c.businessId = b.businessId and c.userId=?";
        List<Cart> list = new ArrayList<>();
        try {
            con = DBUtil.getConnection();
            if (businessId == null) {
                pst = con.prepareStatement(sql);
                pst.setString(1, userId);
            } else {
                pst = con.prepareStatement(sql.concat(" and c.businessId=?"));
                pst.setString(1, userId);
                pst.setInt(2, businessId);
            }
            rs = pst.executeQuery();
            System.out.println(rs.getStatement());
            while (rs.next()) {
                Cart cart = new Cart();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setQuantity(rs.getInt("quantity"));
                cart.setUserId(rs.getString("userId"));

                Business business = new Business();
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessimg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                Food food = new Food();
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));

                cart.setBusiness(business);
                cart.setFood(food);
                list.add(cart);
            }
        } finally {
            DBUtil.close(rs, pst);
        }
        return list;
    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        String sql = "insert into cart(foodId,businessId,userId,quantity) values(?,?,?,?)";
        int row;
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, cart.getFoodId());
            pst.setInt(2, cart.getBusinessId());
            pst.setString(3, cart.getUserId());
            pst.setInt(4, 1);
            row = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return row;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        int row;
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, cart.getQuantity());
            pst.setString(2, cart.getUserId());
            pst.setInt(3, cart.getBusinessId());
            pst.setInt(4, cart.getFoodId());
            row = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return row;
    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        String sql = "delete from cart where businessId=? and userId=?";
        int row;
        try {
            con = DBUtil.getConnection();
            if (cart.getFoodId() == null) {
                pst = con.prepareStatement(sql);
                pst.setInt(1, cart.getBusinessId());
                pst.setString(2, cart.getUserId());
            } else {
                pst = con.prepareStatement(sql.concat(" and foodId=?"));
                pst.setInt(1, cart.getBusinessId());
                pst.setString(2, cart.getUserId());
                pst.setInt(3, cart.getFoodId());
            }
            row = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return row;
    }
}
